package com.gs.spring.core.lifecycle;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

//Component(value="simpleDependency")
public class SimpleDependency 
					implements	BeanPostProcessor 
					, InitializingBean 
					, DisposableBean
					{
	@Autowired	
	public SimpleDependency(String simpleBean) {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " :  constructor DI "+ new Date());
	}
	
	private String simpleCheck;
	private AnotherSimpleBean anotherSimpleBean;
	
	@PostConstruct
	public void postContruction(){		
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " : @PostContruct. Now verify if its ok to use. " + new Date());
	}
	@PreDestroy
	public void preDestruction(){		
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " : @PreDestroy. " + new Date());
	}
	@PreDestroy
	public void anotherPreDestruction(){		
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " : @PreDestroy.anotherPreDestruction " + new Date());
	}
	
	public String init(){
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " init-method " + new Date());
		simpleCheck=" checked in init()";
		return simpleCheck;
	}
	public String factoryDestroy(){
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " destroy-method " + new Date());
		return simpleCheck;
	}
	public void destroy() throws Exception {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " DisposableBean " + new Date());
	}
	

	public String getSimpleCheck() {
		return simpleCheck;
	}
	
	public void setSimpleCheck(String simpleCheck) {
		System.out.print("*********************************");
		this.simpleCheck = simpleCheck;
	}

	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " postProcessAfterInitialization: Bean : " + bean.getClass() + " : beanName " + beanName + " " + new Date());
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " postProcessBeforeInitialization: Bean : " + bean.getClass() + " : beanName " + beanName + " " + new Date());
		simpleCheck = " checked by postProcessBeforeInitialization";
		return bean;
	}

	public void afterPropertiesSet() throws Exception {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " afterPropertiesSet (InitializingBean) " + new Date());
		simpleCheck = " checked by afterPropertiesSet";
	}

	public AnotherSimpleBean getAnotherSimpleBean() {
		return anotherSimpleBean;
	}
	@Autowired
	public void setAnotherSimpleBean(AnotherSimpleBean anotherSimpleBean) {
		System.out.print("*********************************");
		System.out.println(this.getClass().getName() + " : fulfilling setter DI " + new Date());
		this.anotherSimpleBean = anotherSimpleBean;
	}
	
}
